﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WXPackage;
namespace WXTest2.Controllers
{
    public class UserController : Controller
    {
        // GET: User
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取用户OpenID 列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetOpenID()
        {
            return View();
        }


        string appid = "wx145b4a8fd07b24e8";
        string appsecrect = "fe6951dcb99772411c42f724b1336065";
        string redirect_url = "http://wx.jnqianle.com/user/OAuthHandle";
        OAuthManage _oauth = null;
        /// <summary>
        ///控制器构造函数
        /// </summary>
        public UserController()
        {
            _oauth = new OAuthManage(appid, appsecrect, redirect_url);
        }
        /// <summary>
        /// 授权登录
        /// </summary>
        /// <returns></returns>
        public ActionResult AuthLogin()
        {
            ViewBag.url = _oauth.GetCodeUrl();
            return View();
        }
        /// <summary>
        /// 回调处理
        /// </summary>
        /// <returns></returns>
        public ActionResult OAuthHandle()
        {
            string result = "";
            //注册事件处理
            _oauth.OnError = (e) =>
            {
                string msg = "";
                Exception inner = e;
                while (inner != null)
                {
                    msg += inner.Message;
                    inner = inner.InnerException;
                }
                result = msg;
                LogOperate.Write(msg);
            };
            _oauth.OnGetTokenSuccess = (token) =>
            {
                result += "<br/>";
                result += token.ToJsonString();
            };
            _oauth.OnGetUserInfoSuccess = (user) =>
            {
                result += "<br/>";
                result += user.ToJsonString();
            };
            //第二步
            _oauth.GetAccess_Token();
            //第三步
            _oauth.GetUserInfo();
            //显示结果
            ViewBag.msg = result;
            return View();
        }
    }
}